<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="space-between">
          <el-col>
            <el-button type="primary" @click="addFn">添加经济</el-button>
          </el-col>
          <el-col>
            <el-input v-model.trim="inputValue" placeholder="请输入关键字">
              <el-button slot="append" icon="el-icon-search" @click="serchData()" /></el-input>
          </el-col>
        </el-row>

      </el-card>
      <el-card>
        <el-table :data="agentList" border style="width: 100%">
          <el-table-column prop="image" label="图片">
            <template v-slot="{row}">
              <img :src="row.image" class="imgtab">
            </template>
          </el-table-column>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="email" label="邮箱" />
          <el-table-column prop="phone" label="电话" />
          <el-table-column prop="address" label="地址" />
          <el-table-column prop="desc" label="介绍" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" @click="edit(row.id)">编辑</el-button>
              <el-button type="danger" @click="delFn(row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">

          <el-pagination
            layout="prev, pager, next"
            :total="total"
            background
            :page-size="limit"
            @current-change="curr"
          />
        </el-row>
      </el-card>
      <addAgents :show-dialog="showDialog" @closeEvent="showDialog=false" />
    </div>
  </div>
</template>

<script>
import { agentsList, agentsNum, delAgents, serchagents } from '@/api/agents'

import addAgents from './components/addagents.vue'
export default {
  components: { addAgents },
  inject: ['reload'],
  data() {
    return {
      agentList: [],
      total: 0, // 总数
      limit: 5, // 每页显示的条数
      page: 1, // 页码
      start: 0, // 页码第一页
      showDialog: false,
      inputValue: ''

    }
  },

  created() {
    this.getList()
  },
  methods: {
    // 列表总览
    getList() {
      // 获取列表
      this.GetagentsList()
      // 获取数量
      this.agentNumber()
    },
    // 修改
    edit(id) {
      // 弹窗
      this.showDialog = true
      // 回显（弹窗后要在组件中去修改）
      this.$refs.addAgents.updatAgents(id)
    },
    // 查询
    async serchData() {
      const res = await serchagents(this.inputValue)
      this.agentList = res.data
    },
    // 删除
    delFn(id) {
      // console.log(id)
      this.$confirm('是否确定删除', '提示', {
        type: 'warning'
      }).then(async() => {
        // console.log('点击了确定按钮')
        await delAgents(id)
        // this.reload()
        this.getList()
      }).catch((err) => {
        console.log('点击了取消按钮', err)
        this.getList()
      })
      // 刷新列表
      // this.GetagentsList()
    },
    // 添加经济
    addFn() {
      this.showDialog = true
    },
    // 获取列表
    async GetagentsList() {
      const res = await agentsList(
        {
          _limit: this.limit,
          _start: this.start
        }
      )
      console.log('获取列表', res)
      this.agentList = res.data
    },
    // 获取总数量
    async agentNumber() {
      const res = await agentsNum()
      console.log('总数量是', res.data)
      this.total = res.data
    },
    // 翻页
    curr(cur) {
      this.page = cur
      this.start = (this.page - 1) * this.limit
      // 加载列表
      this.getList()
    }

  }
}
</script>

<style lang="scss" scoped>
.imgtab{
  width: 200px;
  height: 200px;

}
</style>
